package com.flux.action

import android.support.v4.util.ArrayMap

/**
 * Object class that hold the type of action and the data we want to attach to it
 */
class RxAction internal constructor(val type: String, val data: ArrayMap<String, Any>, var success: Boolean) {

    operator fun <T> get(tag: String): T {
        @Suppress("UNCHECKED_CAST")
        return data[tag] as T
    }

    //使用静态内部类的方式来构造对象
    class Builder {

        private var type: String? = null
        private var data: ArrayMap<String, Any>? = null

        internal fun with(type: String): Builder {
            this.type = type
            this.data = ArrayMap()
            return this
        }

        fun build(): RxAction {
            if (type == null || type!!.isEmpty()) {
                throw IllegalArgumentException("At least one key is required.")
            }
            return RxAction(type!!, data!!,true)
        }
    }

    companion object {

        fun type(type: String): Builder {
            return Builder().with(type)
        }
    }
}